package com.wxd.boot51.controller;

import com.wxd.boot51.entity.User;
import com.wxd.boot51.entity.Weibo;
import com.wxd.boot51.mapper.WeiboMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author wxd
 * @version 1.0
 */
@RestController
public class WeiboController {
    final
    WeiboMapper weiboMapper;

    public WeiboController(WeiboMapper weiboMapper) {
        this.weiboMapper = weiboMapper;
    }

    @RequestMapping("/weibo/insert")
    public int insert(@RequestBody Weibo weibo, HttpSession session) {
        // 得到当前登录的用户对象
        User u = (User) session.getAttribute("user");
        if (u == null) {
            // 2代表未登录
            return 2;
        }
        // 把当前登录的对象信息 添加到weibo对象中
        weibo.setUserId(u.getId());
        weibo.setNick(u.getNick());
        // new Date()得到当前的系统时间
        weibo.setCreated(new Date());
        System.out.println("weibo = " + weibo);
        weiboMapper.insert(weibo);
        // 1代表发布微博成功
        return 1;
    }

    @RequestMapping("/weibo/select")
    public List<Weibo> select() {
        return weiboMapper.select();
    }

    @RequestMapping("/weibo/deleteById")
    public void deleteById(int id) {
        weiboMapper.deleteById(id);
    }
}
